package com.txw.travel.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.txw.travel.bean.RoleWithStatus;
import com.txw.travel.mapper.AdminMapper;
import com.txw.travel.mapper.RoleMapper;
import com.txw.travel.pojo.Admin;
import com.txw.travel.pojo.Permission;
import com.txw.travel.pojo.Role;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional//add tran
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    public Admin getAdminById(int ids) {

        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("aid", ids);
        Admin admin = adminMapper.selectOne(adminQueryWrapper);
        return admin;
    }

    public Admin getAdminByName(String username) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("username", username);
        Admin admin = adminMapper.selectOne(adminQueryWrapper);
        return admin;
    }

    public Admin getAdminDetail(int id) {
        return adminMapper.getAdminDetail(id);
    }

    public Page<Admin> getAdminList(int index, int size) {
        return adminMapper.selectPage(new Page<Admin>(index, size), null);
    }

    public List<Permission> getAdminPermissions(String username) {
        return adminMapper.getAdminPermissions(username);
    }

    public void insert(Admin admin) {
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminMapper.insert(admin);
    }

    public void update(Admin admin) {
        String oldPassword = adminMapper.selectById(admin.getAid()).getPassword();

        if (!admin.getPassword().equals(oldPassword))
            admin.setPassword(passwordEncoder.encode(admin.getPassword()));

        adminMapper.updateById(admin);
    }

    public void delete(int id) {
        adminMapper.deleteById(id);
    }

    public List<RoleWithStatus> getRoleWithStatusList(Integer aid) {
        //1获取所有角色
        List<Role> roles = roleMapper.selectList(null);
        //判断该admin是否存在权限
        List<Integer> adminExistRoleIdList = roleMapper.getRoleIdByAdminId(aid);

        List<RoleWithStatus> roleWithStatusList = new ArrayList<>();

        for (Role role : roles) {
            RoleWithStatus roleWithStatus = new RoleWithStatus();
            BeanUtils.copyProperties(role, roleWithStatus);
            roleWithStatus.setHasRole(adminExistRoleIdList.contains(role.getRid()));
            roleWithStatusList.add(roleWithStatus);
        }

        return roleWithStatusList;
    }

    //修改用户角色
    public void updateAdminRole(Integer aid, Integer[] rids) {
        adminMapper.deleteAdminAllRoles(aid);

        if (rids == null)
            return;

        for (Integer rid : rids) {
            adminMapper.addAdminRole(aid, rid);
        }
    }

    public void updateAdminStatus(Integer aid) {
        Admin admin = adminMapper.selectById(aid);
        admin.setStatus(!admin.isStatus());
        adminMapper.updateById(admin);
    }
}
